<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    // 首页
    public function index(){
        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        $this->assign('active', 'home');
        $this->display();
    }
    
    // 宝石
    public function diamond($type1="", $type2="",$ptype="", $wtype="", $ctype="")
    {
        // 获取get数据


        $GemType = M('GemType1');
        $GemTypeData = $GemType->select();
        
        $GemType2 = M('GemType2');
        $GemType2Data = $GemType2->select();
        
        $PriceType = M('PriceType');
        $PriceTypeData = $PriceType->select();

        $WeightType = M('WeightType');
        $WeightTypeData = $WeightType->select();

        $CertificateType = M('CertificateType');
        $CertificateTypeData = $CertificateType->select();
        

        // 根据参数查询数据
        $Gem = M('Gem');
        $where = array();
        if ($type1 != "") {
            $where['type1'] = $type1;
            // array_push($where, 'type1', $type1);
        } 
        if ($type2 != "") {
            $where['type2'] = $type2;
            // array_push($where, 'type2', $type2);
        } 
        if ($ptype != '') {
            $where['pricetype'] = $ptype;
            // array_push($where, 'type_price', $ptype);
        } 
        if($wtype != '') {
            $where['weighttype'] = $wtype;
            // array_push($where, 'type_weight', $wtype);
        }
        if ($ctype != '') {
            $where['certificatetype'] = $ctype;
            // array_push($where, 'type_certificate', $ctype);
        }
        // dump($where);
        // die;
        $GemList = $Gem->where($where)->page(1, 8)->select();
        // dump($GemList);
        // die;
        // dump($GemTypeData);die;
        

        $this->assign('type1', $type1);
        $this->assign('type2', $type2);
        $this->assign('ptype', $ptype);
        $this->assign('wtype', $wtype);
        $this->assign('ctype', $ctype);

        $this->assign('gemtypedata', $GemTypeData);
        $this->assign('gemtype2data', $GemType2Data);
        $this->assign('pricetypedata', $PriceTypeData);
        $this->assign('weighttypedata', $WeightTypeData);
        $this->assign('certificatetypedata', $CertificateTypeData);
        $this->assign('active', 'diamond');

        $this->assign('gemlist', $GemList);

        $this->display();
    }
    // 宝石详情
    public function diamondDetial($id='0')
    {
        $Gem = M('Gem');
        $data = '';
        if($id=='0'){
            $data = $Gem
                ->limit(1)
                ->order('updateTime')
                ->join('ckj_gem_type1 ON ckj_gem.type1 = ckj_gem_type1.id')
                ->field('ckj_gem.*,ckj_gem_type1.name as type1name')
                ->find();
        } else {
            $data = $Gem
                ->where(array('ckj_gem.id'=>$id))
                ->field('ckj_gem.*,ckj_gem_type1.name as type1name,ckj_gem_type2.name as type2name,ckj_price_type.name as ptype,ckj_weight_type.name as wtype,ckj_certificate_type.name as ctype')
                ->join('ckj_gem_type1 ON ckj_gem.type1 = ckj_gem_type1.id')
                ->join('ckj_gem_type2 ON ckj_gem.type2 = ckj_gem_type2.id')
                ->join('ckj_price_type ON ckj_gem.pricetype = ckj_price_type.id')
                ->join('ckj_weight_type ON ckj_gem.weighttype = ckj_weight_type.id')
                ->join('ckj_certificate_type ON ckj_gem.certificatetype = ckj_certificate_type.id')
                // ->fetchSql(true)
                ->find();
        }
        // dump($data);
        if($data) {
            $pics = explode(',', $data['pic']);
            $this->assign('active', 'diamond');
            $this->assign('gem', $data);
            $this->assign('pics', $pics);
            $this->display();
        } else {
            $this->error('参数错误', U('diamond'), 1);
        }
    }
    // 定制服务
    public function service()
    {
        $this->assign('active', 'service');
        $this->display();
    }
    // 全球猎宝
    public function purchasing()
    {
        if(IS_POST) {
            $data['type'] = I('post.type');
            $data['price'] = I('post.price');
            $data['uname'] = I('post.uname');
            $data['phone'] = I('post.phone');
            // dump($data);die;
            $Purchasing = M('Purchasing');
            $rs = $Purchasing->data($data)->add();
            if ($rs) {
                $this->success('提交成功', U('purchasing'));
            } else {
                $this->error('提交失败', U('purchasing'));
            }
        } else {
            $this->assign('active', 'purchasing');
            $this->display();
        }
    }
    // 彩矿家日记
    public function journal($t=0)
    {
        $type = I('get.t');
        $NewsType = M('NewsType');
        $ntype = $NewsType->select();
        // dump($type);
        // die;
        $News = M('News');
        $where = array('type'=>$t);
        if ($t == 0) {
            $where = '';
        }
        $list = $News
            ->where($where)
            ->order('updateTime')
            ->page(1, 10)
            ->join('ckj_news_type ON ckj_news.type = ckj_news_type.id')
            ->field('ckj_news.*,ckj_news_type.name as typename')
            // ->fetchSql(true)
            ->select();
        // dump($list);
        $this->assign('active', 'journal');
        $this->assign('ntype', $ntype);
        $this->assign('cutype', $type);
        $this->assign('list', $list);
        $this->display();
    }
    // 彩矿家日记详情
    public function journalDetial($id=0)
    {
        $News = M('News');
        $data = '';
        if($id==0){
            $data = $News->limit(1)->order('updateTime')->find();
        } else {
            $data = $News->where(array('id'=>$id))->find();
        }
        // dump($data[0]);die;
        if($data) {
            $this->assign('active', 'journal');
            $this->assign('news', $data);
            $this->display();
        } else {
            $this->error('参数错误', U('journal'), 1);
        }
    }
    // 登录
    public function login($username='', $password='')
    {
        if(IS_POST) {
            if(trim($username) =='' || trim($password)== ''){
                $this->error('用户名或密码不能为空', U('login'));
            } else {
                $User = M('User');
                $userData = $User->where(array('username'=>$username))->select();
                // dump($userData);die;
                if (sizeof($userData) > 0) {
                    if ($userData[0]['password'] === md5($password)) {
                        $this->success('登录成功', U('index'));
                    } else {
                        $this->error('密码错误', U('login'));
                    }
                }
            }
        } else {
            $this->display();
        }
    }
    // 注册
    public function register($username='', $password='', $repassword='')
    {
        if(IS_POST) {
            if(trim($username) =='' || trim($password)== ''){
                $this->error('用户名或密码不能为空', U('login'));
            } else if(trim($password) != trim($repassword)) {
                $this->error('两次密码不一致', U('login'));
            } else {
                $User = M('User');
                $data = $User->where(array('username'=>$username))->select();
                if (sizeof($data) > 0) {
                    $this->error('该用户已经存在', U('login'));
                } else {
                    $userData['username'] = trim($username);
                    $userData['password'] = md5(trim($password));
                    $User->data($userData)->add();
                    $this->success('注册成功!', U('login'));
                }
                
            }
        }
    }

}